package cd.zhixuan.utils;

import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.res.Configuration;
import android.util.DisplayMetrics;

/**
 * @author CHAO
 * Create Time: 2021.12.28
 **/
public class UITools {

    public static int dp2px(Context mContext, int dp) {
        DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
        return (int) (dp * displayMetrics.density + 0.5f);
    }

    public static int px2dp(Context mContext, int px) {
        DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
        return (int) (px / displayMetrics.density + 0.5f);
    }

    public static int px2sp(Context mContext, int px) {
        DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
        return (int) (px / displayMetrics.scaledDensity + 0.5f);
    }

    public static int sp2px(Context mContext, int sp) {
        DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
        return (int) (sp * displayMetrics.scaledDensity + 0.5f);
    }

    public static int dp2sp(Context mContext, int dp) {
        DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
        return (int) (dp * displayMetrics.density / displayMetrics.scaledDensity + 0.5f);
    }

    public static int sp2dp(Context mContext, int sp) {
        DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
        return (int) (sp * displayMetrics.scaledDensity / displayMetrics.density + 0.5f);
    }


    // 系统的Density
    private static float sNoncompatDensity;
    // 系统的ScaledDensity
    private static float sNoncompatScaledDensity;

    //屏幕宽高
//    private static final float SCREEN_WIDTH = 1920f;
//    private static final float SCREEN_HEIGHT = 720f;

    //切图参照宽高
    private static final float UI_WIDTH = 640f;
    private static final float UI_HEIGHT = 360f;

    /* *
     *
     */
    public static void setCustomDensity(Context activity, final Context application) {
        // 16:9、4:3 高宽比
//        boolean isValidRatio = false;
//        Logger.d("screen width pixel: " + SCREEN_WIDTH);
//        Logger.d("screen height pixel: " + SCREEN_HEIGHT);
        DisplayMetrics displayMetrics = application.getResources().getDisplayMetrics();
        if (sNoncompatDensity == 0) {
            sNoncompatDensity = displayMetrics.density;
            sNoncompatScaledDensity = displayMetrics.scaledDensity;
            // 监听在系统设置中切换字体
            application.registerComponentCallbacks(new ComponentCallbacks() {
                @Override
                public void onConfigurationChanged(Configuration newConfig) {
                    if (newConfig != null && newConfig.fontScale > 0) {
                        sNoncompatScaledDensity = application.getResources().getDisplayMetrics().scaledDensity;
                    }
                }

                @Override
                public void onLowMemory() {

                }
            });
        }
        // 此处以400dp的设计图作为例子
        float targetDensity = 1.0f;

        //横屏，则以高度维度去适配
        targetDensity = Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels) / Math.min(UI_WIDTH, UI_HEIGHT);


        float targetScaledDensity = targetDensity * (sNoncompatScaledDensity / sNoncompatDensity);
        int targetDensityDpi = (int) (DisplayMetrics.DENSITY_DEFAULT * targetDensity);
//        int targetDensityDpi = (int) (160 * targetDensity);


        LogTools.d("targetDensity: " + targetDensity);
        LogTools.d("targetScaledDensity: " + targetScaledDensity);
        LogTools.d("targetDensityDpi: " + targetDensityDpi);


        displayMetrics.density = targetDensity;
        displayMetrics.scaledDensity = targetScaledDensity;
        displayMetrics.densityDpi = targetDensityDpi;

        DisplayMetrics activityDisplayMetrics = activity.getResources().getDisplayMetrics();
        activityDisplayMetrics.density = targetDensity;
        activityDisplayMetrics.scaledDensity = targetScaledDensity;
        activityDisplayMetrics.densityDpi = targetDensityDpi;
    }
}
